﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Net;
using System.Net.Mail;

public partial class MyExams : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SqlConnection conn;
            string connectionString = ConfigurationManager.ConnectionStrings["ExamScheduleDBConString"].ConnectionString;
            conn = new SqlConnection(connectionString);
            SqlCommand comm = new SqlCommand("sp_getExamListByProfessor", conn);
            comm.CommandType = System.Data.CommandType.StoredProcedure;
            comm.Parameters.Add("@profUserName",
               System.Data.SqlDbType.Text);
            comm.Parameters["@profUserName"].Value = Session["UserName"];
            SqlDataReader reader;

            try
            {
                conn.Open();
                reader = comm.ExecuteReader();
                gridMyExams.DataSource = reader;
                gridMyExams.DataBind();

            }
            finally
            {
                conn.Close();
            }
        }
    }
    protected void selectRow(object sender, EventArgs e)
    {
            SqlConnection conn;
            string connectionString = ConfigurationManager.ConnectionStrings["ExamScheduleDBConString"].ConnectionString;
            conn = new SqlConnection(connectionString);
            SqlCommand comm = new SqlCommand("SELECT * FROM ExamDetails WHERE CourseID = " + gridMyExams.SelectedValue, conn);
            SqlDataReader reader;
            conn.Open();
            reader = comm.ExecuteReader();
            detailsMyExams.DataSource = reader;
            detailsMyExams.DataBind();
            reader.Close();
            conn.Close();

    }
    protected void detailsMyExams_ItemCommand(object sender, DetailsViewCommandEventArgs e)
    {
        SqlConnection conn;
        string connectionString = ConfigurationManager.ConnectionStrings["ExamScheduleDBConString"].ConnectionString;
        conn = new SqlConnection(connectionString);
        SqlCommand updateComm = new SqlCommand("UPDATE ExamDetails SET Additional_InvigilationRequested = 1, RequestFulfilled = 0 WHERE CourseID = " + gridMyExams.SelectedValue, conn);
        conn.Open();
        updateComm.ExecuteNonQuery();
        SqlCommand comm = new SqlCommand("SELECT Professor.Email, Course.CourseName, Course.CourseDescript "
        +"FROM Course INNER JOIN Professor ON Course.ProfessorID = Professor.ProfessorID "
        +"WHERE Course.CourseID = " + gridMyExams.SelectedValue, conn);
        SqlDataReader reader;
        reader = comm.ExecuteReader();

        while (reader.Read())
        {
            MailMessage message = new MailMessage();
            SmtpClient client;
            message.Priority = MailPriority.Normal;
            message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
            message.From = new MailAddress("info@sws320developmentproject.com");
            client = new SmtpClient();
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.Host = "mail.sws320developmentproject.com";
            client.Port = 25;
            client.EnableSsl = false;
            message.To.Add(new MailAddress(reader.GetValue(0) + ""));
            message.Subject = "SWS320 Development Project - Email - Request For Additional Invigilation - " + reader.GetSqlValue(1);
            message.Body = "THIS IS JUST FOR TESTING PURPOSES." +
                "Your request for additional invigilation for " + reader.GetSqlValue(2) + " has been received.";
            client.Credentials = new NetworkCredential("info@sws320developmentproject.com ", "INFO320");

            client.Send(message);
            Label1.Text = "Request Completed. Confirmation Email sent to " + reader.GetValue(0) + "";
            detailsMyExams.Visible = false;
            gridMyExams.Visible = false;
        }
        reader.Close();
        conn.Close();

    }
}